CC = gcc
ASM = nasm
LD = ld
MOUNT_POINT = /mnt/LiteOS

C_SOURCES = $(shell find . -name "*.c")
C_OBJECTS = $(patsubst %.c, %.o, $(C_SOURCES))
CC_FLAGS = -c -Wall -m32 -ggdb -gstabs+ -nostdinc -fno-builtin -fno-stack-protector -I include
LD_FLAGS = -T link.ld -m elf_i386 -nostdlib

all: boot.o $(C_OBJECTS) link build_img

.c.o:
	@echo '----------build kernnel src...'
	$(CC) -masm=intel $(CC_FLAGS) $< -o $@

boot.o: boot.asm
	@echo '----------build boot...'
	$(ASM) -f elf boot.asm

.PHONY: kernel
link:
	@echo '----------linking...'
	$(LD) $(LD_FLAGS) boot.o  $(C_OBJECTS) -o kernel

.PHONY: build_img
build_img:
	@echo '----------build OS image...'
	@if [ ! -d $(MOUNT_POINT) ]; then mkdir $(MOUNT_POINT); fi  # 挂载点不存在则创建
	sudo mount LiteOS.img /mnt/LiteOS
	sudo cp kernel /mnt/LiteOS/kernel
	sudo cp floppy/boot/grub/grub.conf /mnt/LiteOS/boot/grub/grub.conf
	sudo cp floppy/boot/grub/menu.lst /mnt/LiteOS/boot/grub/menu.lst
	
	sleep 1
	sudo umount /mnt/LiteOS

.PHONY:qemu
qemu:
 
.PHONY: clean
clean:
	rm *.o kernel
	rm lib/*.o
